package com.gupt.reggie.controller;/*
@author name
@data 2023/4/19-16:37
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gupt.reggie.common.R;
import com.gupt.reggie.entity.Category;
import com.gupt.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    /**
     * 保存当前添加的菜品类型
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("新增分类");
        categoryService.save(category);
        return R.success("新增分类成功");    //若有重复分类（抛出异常，则会被common下面的GlobalExceptionHandler捕获
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        Page pageInfo = new Page(page,pageSize);

        //创建条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);//升序

       pageInfo =  categoryService.page(pageInfo,queryWrapper);

       return R.success(pageInfo);
    }

    /**
     * 删除分类
     * @param ids
     * @return
     */
    @DeleteMapping              //ids 无需对应前端?
    public R<String> deleteCategoryById(Long ids){   //不能用int声明，无法将其化为null ？？？
        log.info("删除分类id：{}",ids);
        categoryService.removeById(ids);
        return R.success("删除成功");
    }


    @PutMapping
    public R<String> updateCategoryById(@RequestBody Category category){
        log.info("修改类型：{}",category);

        categoryService.updateById(category);
        return R.success("修改成功");
    }


    @GetMapping("/list")
    public R<List<Category>> retrieveCategoryById(Category category){  //接收前端的type=1 数据，不为json 不用加RequestBody注解
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();

//        //条件
//        if(category != null){
//            lambdaQueryWrapper.eq(Category::getType,category.getType());
//            //排序 根据排序参数字段，创建时间进行排序
//            lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);
//
//            List<Category> list = categoryService.list(lambdaQueryWrapper);
//            return R.success(list);
//        }
//            return R.error("系统错误");

        //条件

        if(category.getType() != null){
            lambdaQueryWrapper.eq(Category::getType,category.getType());
            //排序 根据排序参数字段，创建时间进行排序
            lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);

            List<Category> list = categoryService.list(lambdaQueryWrapper);
            return R.success(list);
        }else {
            List<Category> list = categoryService.list();
            return R.success(list);
        }
    }
}
